// var sound_playing = false
// var sound = new Audio('./tmall.mp3')
// sound.preload = true
// sound.loop = true
// sound.autoplay = false

// $(sound).on('play', function () {
//   sound_playing = true
// })

var isPaused = false

function getImage () {
  // var host = window.location.host
  return 'https://618report.lorealchina.com/wechat/tmall/img/TM_618.jpg'
}

function getLink () {
  return window.location.href
}

function getMessage () {
  return '欧莱雅集团，高燃618'
}

$(function () {
  // $('.soundon').show()
  // $('.sound').on('touchstart', function () {
  //   $('.sound img').hide()
  //   if (sound.paused) {
  //     sound.play()
  //     $('.soundon').show()
  //   } else {
  //     sound.pause()
  //     $('.soundoff').show()
  //   }
  // })
  $('#audio')[0].play()
  $('.soundon').show()
  $('.sound').on('touchstart', function () {
    $('.sound img').hide()
    if (isPaused) {
      isPaused = false

      $('#audio')[0].play()
      $('.soundon').show()
    } else {
      $('#audio')[0].pause()
      isPaused = true
      $('.soundoff').show()
    }
  })

  $.post(
    'https://618report.lorealchina.com/wechat/getsign/getSign',
    {
      url: encodeURIComponent(window.location.href.split('#')[0])
    },
    function (item) {
      if (item && item.appId) {
        onWechatApiInt(item)
      }
    },
    'json'
  )
})

function onWechatApiInt (signPackage) {
  var debug = false
  if (window.location.href.indexOf('test') > -1) {
    debug = true
  }
  wx.config({
    debug: debug,
    appId: signPackage.appId,
    timestamp: signPackage.timestamp,
    nonceStr: signPackage.noncestr,
    signature: signPackage.signature,
    jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage']
  })

  wx.ready(function () {
    onWeichatShare()
    // 视频音频自动播放的放在这里
    $('#audio')[0].play()
  })

  document.addEventListener('WeixinJSBridgeReady', function () {}, false)
}

function onWeichatShare () {
  wx.onMenuShareTimeline({
    title: getMessage(),
    link: getLink(),
    imgUrl: getImage(),
    success: function () {},
    cancel: function () {}
  })

  wx.onMenuShareAppMessage({
    title: getMessage(),
    desc: '天猫618',
    link: getLink(),
    imgUrl: getImage(),
    type: '',
    dataUrl: '',
    success: function () {},
    cancel: function () {}
  })
}
